#include "stdafx.h"
#include "player_manager.h"


CPlayerMgr::CPlayerMgr(void)
{

}

CPlayerMgr::~CPlayerMgr(void)
{
	m_container.clear();
}

player::pointer CPlayerMgr::CreatePlayer( uint64 _id , const wstring & _name )
{
	player::pointer _ptr(new player(_id,_name));
	player_iterator it = m_container.insert(_ptr).first;

	if ( it != m_container.end() )
	{
		return _ptr;
	}
	else
	{
#ifdef RUNTIME_CHECK
		throw std::runtime_error( "player ID allready exist, please check!" );
#endif
		return Find(_id);
	}
}

void CPlayerMgr::DestoryPlayer( player::pointer _ptr )
{
	if ( Find(_ptr->get_id()) != NULL )
	{
		m_container.erase(_ptr->get_id());
		return;
	}
#ifdef RUNTIME_CHECK
	throw std::runtime_error( "player not exist!");
#endif
}

player::pointer CPlayerMgr::Find( uint64 _id )
{
	player_id_iterator it = m_container.get<index_by_id>().find(_id);

	if ( it == m_container.get<index_by_id>().end() )
	{
		return NULL;
	}
	else
	{
		return *it;
	}
}

player::pointer CPlayerMgr::Find(const wstring & _name )
{
	player_name_iterator it = m_container.get<index_by_name>().find(_name);

	if ( it == m_container.get<index_by_name>().end() )
	{
		return NULL;
	}
	else
	{
		return *it;
	}
}
